// store/user/index.ts
import {
  VuexModule,
  Module,
  Mutation,
  Action,
  getModule
} from 'vuex-module-decorators'
import store from '@/store'
import { UserState, userInfo } from './types'
// import { getToken, setToken, removeToken } from '@/utils/cookies'

// store/user/index.ts

@Module({ dynamic: true, store, name: 'user' })
class User extends VuexModule implements UserState {
  public login = false
  public token = ''
  public roles: string[] = []
  public userInfo = {
    name: '',
    mobile: '',
    email: '',
    avatar: '',
    introduction: '',
    notificationCount: 0
  }

  @Mutation
  private SET_TOKEN(token: string) {
    this.token = token
  }

  @Mutation
  private SET_NAME(name: string) {
    this.userInfo.name = name
  }

  @Mutation
  private SET_AVATAR(avatar: string) {
    this.userInfo.avatar = avatar
  }

  @Mutation
  private SET_INTRODUCTION(introduction: string) {
    this.userInfo.introduction = introduction
  }

  @Mutation
  private SET_ROLES(roles: string[]) {
    this.roles = roles
  }

  @Mutation
  private SET_EMAIL(email: string) {
    this.userInfo.email = email
  }

  @Mutation
  private SET_USERINFO(userInfo: userInfo) {
    this.userInfo = userInfo
  }

  @Action
  public async Login(userProfile: { username: string, password: string }) {
    //  axios({
    //         url: 'https://....'
    //     }).then((response) => {
    //         const payload: User = response && response.data;
    //         commit('profileLoaded', payload);
    //     }, (error) => {
    //         console.log(error);
    //         commit('profileError');
    //     });

    // let { username, password } = userInfo
    // username = username.trim()
    // const { data } = await login({ username, password })
    // setToken(data.accessToken)
    this.SET_TOKEN('111')
    this.SET_USERINFO({
      name: 'Maxotm',
      mobile: '15979076441',
      email: '1635358408@qq.com',
      avatar: '@/assets/logo.png',
      introduction: '111',
      notificationCount: 0
    })
  }

  @Action
  public async LogOut() {
    if (this.token === '') {
      throw Error('LogOut: token is undefined!')
    }
    // await logout()
    // removeToken()
    // resetRouter()

    // Reset visited views and cached views
    this.SET_TOKEN('')
    this.SET_ROLES([])
  }

  @Action
  public ResetToken() {
    // removeToken()
    this.SET_TOKEN('')
    this.SET_ROLES([])
  }
  // @Action
  // public async GetUserInfo() {
  //   if (this.token === '') {
  //     throw Error('GetUserInfo: token is undefined!')
  //   }
  //   const { data } = await getUserInfo({ /* Your params here */ })
  //   if (!data) {
  //     throw Error('Verification failed, please Login again.')
  //   }
  //   const { roles, name, avatar, introduction, email } = data.user
  //   // roles must be a non-empty array
  //   if (!roles || roles.length <= 0) {
  //     throw Error('GetUserInfo: roles must be a non-null array!')
  //   }
  //   this.SET_ROLES(roles)
  //   this.SET_NAME(name)
  //   this.SET_AVATAR(avatar)
  //   this.SET_INTRODUCTION(introduction)
  //   this.SET_EMAIL(email)
  // }
}

export const UserModule = getModule(User)
